﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MetroCashLib.DataInfo;

namespace MetroCashLib.DataBussiness
{
    public class BAccount
    {
        public static List<Account> getCommonAccounts(string profileName)
        {
            try
            {
                using (var db = new SQLite.SQLiteConnection(Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, Path.Combine(SettingStrings.PROFILE_FOLDER_NAME, profileName))))
                {
                    return db.Query<Account>("select * from Account where AccountParentId IS NULL").ToList<Account>();
                }
            }
            catch
            {
                return new List<Account>();
            }
        }

        public static List<Account> getChildAccounts(string profileName, Account parrent)
        {
            try
            {
                using (var db = new SQLite.SQLiteConnection(Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, Path.Combine(SettingStrings.PROFILE_FOLDER_NAME, profileName))))
                {
                    return db.Query<Account>("select * from Account where AccountParentId="+parrent.Id).ToList<Account>();
                }
            }
            catch
            {
                return new List<Account>();
            }
        }

        public static int createAccount(string profileName, Account account)
        {
            try
            {
                using (var db = new SQLite.SQLiteConnection(Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, Path.Combine(SettingStrings.PROFILE_FOLDER_NAME, profileName))))
                {
                    return db.Insert(account);
                }
            }
            catch
            {
                return -1;
            }
        }

        public static int updateAccount(string profileName, Account account)
        {
            try
            {
                using (var db = new SQLite.SQLiteConnection(Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, Path.Combine(SettingStrings.PROFILE_FOLDER_NAME, profileName))))
                {
                    return db.Update(account);
                }
            }
            catch
            {
                return -1;
            }
        }

        public static int deleteAccount(string profileName, Account account)
        {
            try
            {
                using (var db = new SQLite.SQLiteConnection(Path.Combine(Windows.Storage.ApplicationData.Current.RoamingFolder.Path, Path.Combine(SettingStrings.PROFILE_FOLDER_NAME, profileName))))
                {
                    return db.Delete(account);
                }
            }
            catch
            {
                return -1;
            }
        }
    }
}
